<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class video extends CI_Controller{
      protected $_templates;
      function __construct() {
          parent::__construct();
          $this->pre_message = "";         
          $this->permit_lib->check_permit();
          $this->session->set_userdata(array('Url'=>uri_string()));
          $this->lang->load('video');
		  $this->load->model('video_model','video');
		  $this->load->helper('upload_file');
		  $this->load->library('image_library');
      }   
	  function index(){
		  $this->listvideo();
	  }
      function listvideo(){
         
        /*Begin search*/
            $type = $this->input->post('type');
            $data['search_bar']=searchBar(array(),array('name'=>'Tiêu video'),'video/listvideo/');
            if($type=='ajax')
            {
                $arr_search['key_search'] = $this->input->post('key_search');
                $arr_search['field_search'] = $this->input->post('field_search');
                $this->session->set_userdata('key_search',$arr_search['key_search']);
                $this->session->set_userdata('field_search',$arr_search['field_search']);
               
            }
            else{
                $arr_search['key_search']=$this->session->userdata('key_search');
                $arr_search['field_search']=$this->session->userdata('field_search');
            }
            /*End  search*/
            $data['title'] = 'Danh sách Video';
            $data['add'] = 'video/add';
            $config['base_url'] = base_url().'video/listvideo/'.(int)$this->uri->segment(3);  
            $data['num'] = $this->video->get_num_data($arr_search);
            $config['total_rows']   =  $data['num'];
            $config['per_page']  =   '20';
            $config['uri_segment'] = 4;   
            $this->pagination->initialize($config);   
            $data['list'] =   $this->video->get_all_data($config['per_page'],(int)$this->uri->segment('4'),$arr_search);
            $data['pagination']    = $this->pagination->create_links();            
            $this->_templates['page'] = 'video/listvideo';
           if($type=="ajax")
                echo $this->load->view($this->_templates['page'],$data,true);
            else
                    $this->site_lib->load($this->_templates['page'],$data); 
      }
     
  
      function add(){                         
            $data['title'] = 'Thêm mới Video';
            $this->form_validation->set_rules('video_name',lang('video_name'),'trim|required');
			$this->form_validation->set_rules('video_name_en',lang('video_name_en'),'trim|required');
         
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				 $uploadDir = "../uploads/images/video/";
				 $files = $_FILES['uploadfile'];
				 $link = $_POST['link'];
				 if($link==''){
					$video_name = settings_upload_avatar($files,$uploadDir,2);
					 if($video_name!='-1' ){
						$arr_video = array('name_file'=>$video_name);
					}else{
						$this->pre_message ="File không đúng định dạng, vui lòng tải lại";
					}
				  }
					// upload hinh dai dien
					$dirFile = "../uploads/images/images_lib/";
					$dirFileNew = "../uploads/images/video/small/";
					$dirFileMedium = "../uploads/images/video/medium/";
					$filename =$_POST['lb_image'];
					$dirFileRoot =$dirFile.$filename;
					$dirFileResizeSmall = $dirFileNew.$filename;
					$dirFileResizeMedium = $dirFileMedium.$filename;
					$img_name =$this->image_library->resizeThumbnailImage($dirFileResizeSmall,$dirFileRoot,198,131,0,0,0);
					$img_name =$this->image_library->resizeThumbnailImage($dirFileResizeMedium,$dirFileRoot,515,415,0,0,0);
					$filename==''? $img_name='':$img_name=$filename;
					$arr_image = array('image'=>$filename); 
					$data = $this->build_data($_POST,1);
					$data  = array_merge($data,$arr_image); 
					if($arr_video!='-1' && $link==''){
						$data  = array_merge($data,$arr_video); 
					}
					if($this->video->saveData(0,$data)){
						 $this->session->set_flashdata('message','Lưu thành công');
						 redirect('video/listvideo');
					 }else{
						 $this->pre_message = 'Lưu không thành công';
					 }
          	}
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'video/add';
          $this->site_lib->load($this->_templates['page'],$data);
      }
       function edit($id){                         
            $data['title'] = 'Sửa Video';
            $data['query'] = $this->video->get_object($id);
            $this->form_validation->set_rules('video_name',lang('video_name'),'trim|required');
			$this->form_validation->set_rules('video_name_en',lang('video_name_en'),'trim|required');
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				 $uploadDir = "../uploads/images/video/";
				 $files = $_FILES['uploadfile'];
				 $link = $_POST['link'];
				 if($link==''){	
					if($files['name']!=''){
						$video_name = settings_upload_avatar($files,$uploadDir,2);
						 unlink("../uploads/images/video/".$data['query']->name_file);
					}else
						$video_name =$data['query']->name_file;
				  }
					 if($video_name!='-1' ){
						$arr_video = array('name_file'=>$video_name);
					}else{
						$this->pre_message ="File không đúng định dạng, vui lòng tải lại";
					}
					// upload hinh dai dien
					 $dirFile = "../uploads/images/images_lib/";
					 $dirFileNew = "../uploads/images/video/small/";
					 $dirFileNewMedium = "../uploads/images/video/medium/";
					 $filename =$_POST['lb_image'];
					 if($filename!=''){
						 $image_old=$data['query']->image;
						 unlink("../uploads/images/video/small/".$image_old);
						 unlink("../uploads/images/video/medium/".$image_old);
						 $dirFileRoot =$dirFile.$filename;
						 $dirFileResizeSmall = $dirFileNew.$filename;
						 $dirFileResizeMedium= $dirFileNewMedium.$filename;
						 $img_name =$this->image_library->resizeThumbnailImage($dirFileResizeSmall,$dirFileRoot,198,131,0,0,0);
						  $img_name =$this->image_library->resizeThumbnailImage($dirFileResizeMedium,$dirFileRoot,515,415,0,0,0);
					}
					
					$filename=$img_name==''?$data['query']->image:$filename;
					$arr_image = array('image'=>$filename); 
					$data = $this->build_data($_POST,1);
					$data  = array_merge($data,$arr_image); 
					if($img_name!='-1'){
						$data  = array_merge($data,$arr_video); 
					}
					if($this->video->saveData($id,$data)){
						 $this->session->set_flashdata('message','Lưu thành công');
						 redirect('video/listvideo');
					 }else{
						 $this->pre_message = 'Lưu không thành công';
					 }
          	}
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'video/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function del(){
        $id = $this->uri->segment(3);
        $page = $this->uri->segment(4);

        if($this->video->deleted($id))
            $this->session->set_flashdata('message','Đã xóa thành công');
        else $this->session->set_flashdata('message','Xóa không thành công');
        
        redirect('video/listvideo/'.$page);
    }
    function dels(){
        if(!empty($_POST['ar_id']))
        {
            $page = (int)$this->input->post('page');
            $ar_id = $this->input->post('ar_id');

            if(!empty($_POST['btn_submit']))
            {
                for($i = 0; $i < sizeof($ar_id); $i ++) {
                    if ($ar_id[$i]){
                        if($this->video->deleted($ar_id[$i]))
                            $this->session->set_flashdata('message','Đã xóa thành công');
                        else $this->session->set_flashdata('message','Xóa không thành công');
                    }
                }
            }
        }
        redirect('video/listvideo/'.$page);
    }
    function build_data($data,$flag=1)
	{//flag=1:0?insert:update
	
				$dataUpdate["name"]=trim($data["video_name"]);
				$dataUpdate["name_en"]=trim($data["video_name_en"]);
                $dataUpdate["description"]=trim($data["video_description"]);
                $dataUpdate["description_en"]=trim($data["video_description_en"]);
				$dataUpdate["present"]=trim($data["present"]);
				$dataUpdate["author"]=trim($data["author"]);
				$dataUpdate["link"]=trim($data["link"]);
				if($flag==1)
               		$dataUpdate["dt_create"] = date('Y-m-d H:s:i');
                if(isset($data["bl_active"])){
			$dataUpdate["bl_active"]=trim($data["bl_active"]);
		}else{
			$dataUpdate["bl_active"]=0;
		}
		return $dataUpdate;
	}

  }
?>
